package com.ruoyi.carrepair.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.carrepair.domain.QxOperatelog;
import com.ruoyi.carrepair.mapper.QxOperatelogMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.zjc.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.carrepair.mapper.QxPeijianMapper;
import com.ruoyi.carrepair.domain.QxPeijian;
import com.ruoyi.carrepair.service.IQxPeijianService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 配件管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-19
 */
@Service
public class QxPeijianServiceImpl implements IQxPeijianService 
{
    @Autowired
    private QxPeijianMapper qxPeijianMapper;
    @Autowired
    private QxOperatelogMapper qxOperatelogMapper;

    /**
     * 查询配件管理
     * 
     * @param id 配件管理主键
     * @return 配件管理
     */
    @Override
    public QxPeijian selectQxPeijianById(String id)
    {
        return qxPeijianMapper.selectQxPeijianById(id);
    }

    /**
     * 查询配件管理列表
     * 
     * @param qxPeijian 配件管理
     * @return 配件管理
     */
    @Override
    public List<QxPeijian> selectQxPeijianList(QxPeijian qxPeijian)
    {
        try{
            String toApplet = SecurityUtils.getLoginUser().getToApplet();
            qxPeijian.setToApplet(toApplet);
        }catch (Exception ex) {

        }
        return qxPeijianMapper.selectQxPeijianList(qxPeijian);
    }

    /**
     * 新增配件管理
     * 
     * @param qxPeijian 配件管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertQxPeijian(QxPeijian qxPeijian)
    {
        String toapplet = SecurityUtils.getLoginUser().getToApplet();
        Long createUserId = SecurityUtils.getLoginUser().getUserId();
        String createUserName = SecurityUtils.getLoginUser().getUsername();

        qxPeijian.setIsDeleted(0L);
        qxPeijian.setRowVersion(0L);
        qxPeijian.setToApplet(SecurityUtils.getLoginUser().getToApplet());
        qxPeijian.setCreateID(SecurityUtils.getLoginUser().getUserId());
        qxPeijian.setCreator(SecurityUtils.getLoginUser().getUsername());
        qxPeijian.setCreateDate(new Date());
        qxPeijian.setId(String.valueOf(new SnowflakeIdWorker(5,8).nextId()));
        int num = qxPeijianMapper.insertQxPeijian(qxPeijian);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(createUserName + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "新增了一条配件信息：【" + qxPeijian.getPname()+"】");
        qxOperatelog.setCategory("1");
        qxOperatelog.setMenuModel("配件管理");
        qxOperatelog.setCreateID(createUserId);
        qxOperatelog.setCreator(createUserName);
        qxOperatelog.setToApplet(toapplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 修改配件管理
     * 
     * @param qxPeijian 配件管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateQxPeijian(QxPeijian qxPeijian)
    {
        String toapplet = SecurityUtils.getLoginUser().getToApplet();
        Long createUserId = SecurityUtils.getLoginUser().getUserId();
        String createUserName = SecurityUtils.getLoginUser().getUsername();
        int num = qxPeijianMapper.updateQxPeijian(qxPeijian);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(createUserName + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "更新了一条配件信息：【" + qxPeijian.getPname()+"】");
        qxOperatelog.setCategory("2");
        qxOperatelog.setMenuModel("配件管理");
        qxOperatelog.setCreateID(createUserId);
        qxOperatelog.setCreator(createUserName);
        qxOperatelog.setToApplet(toapplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);
        return num;
    }

    /**
     * 批量删除配件管理
     * 
     * @param ids 需要删除的配件管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteQxPeijianByIds(String[] ids)
    {
        String idsStr="";
        for(String id:ids){
            idsStr = idsStr + id + ",";
        }

        String toApplet = SecurityUtils.getLoginUser().getToApplet();
        Long createID = SecurityUtils.getLoginUser().getUserId();
        String creator = SecurityUtils.getLoginUser().getUsername();
        int num = qxPeijianMapper.deleteUpdQxPeijianByIds(ids);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(creator + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "删除了配件信息。配件id：【" + idsStr+ "】");
        qxOperatelog.setCategory("3");
        qxOperatelog.setMenuModel("配件管理");
        qxOperatelog.setCreateID(createID);
        qxOperatelog.setCreator(creator);
        qxOperatelog.setToApplet(toApplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 删除配件管理信息
     * 
     * @param id 配件管理主键
     * @return 结果
     */
    @Override
    public int deleteQxPeijianById(String id)
    {
        return qxPeijianMapper.deleteQxPeijianById(id);
    }
}
